<?php
namespace database;

use sql\SqlExec;
use model\FileInf;
/*
	说明：
		1.在调用此函数前不能有任何输出操作。比如 echo print
		
	更新记录：
		2012-04-03 创建
		2014-08-11 更新数据库操作代码。 
		2017-05-31 精简代码
*/
class DBFolder
{
		
	function __construct() 
	{
	}
	
	function Complete($id,$uid)
	{
	    $sql = "update up7_files set f_lenSvr=f_lenLoc,f_complete=1,f_perSvr='100%' where (f_id=:f_id or f_pidRoot=:f_id) and f_uid=:f_uid;";
	    $se = new SqlExec();
	    $se->exec("up7_files", $sql, null, "f_id,f_uid", array("f_id"=>$id,"f_uid"=>$uid));
	    $se->update2("up7_folders", array("f_complete"=>1), array("f_id"=>$id,"f_uid"=>$uid));
	}
	
	function add($inf/*FileInf*/)
	{
		$sb = "insert into up7_folders(
				 f_id
				,f_uid
				,f_nameLoc
				,f_pathLoc
				,f_pathSvr
				,f_pathRel
				,f_fileCount	
				) values (	
				 :f_id
				,:f_uid
				,:f_nameLoc				
				,:f_pathLoc
				,:f_pathSvr
				,:f_pathRel
				,:f_fileCount
				)";
	
		$db = new DbHelper();
		$cmd = $db->prepare_utf8( $sb );
	
		$cmd->bindParam(":f_id"     ,$inf->id);
		$cmd->bindValue(":f_uid"    ,$inf->uid);
		$cmd->bindParam(":f_nameLoc"   ,$inf->nameLoc);		
		$cmd->bindParam(":f_pathLoc",$inf->pathLoc);
		$cmd->bindParam(":f_pathSvr",$inf->pathSvr);
		$cmd->bindParam(":f_pathRel",$inf->pathRel);
		$cmd->bindParam(":f_fileCount"  ,$inf->fileCount);
		$cmd->execute();
	}
	
	static function read($id)
	{	    
	    $sql = "select
                 f_id
		 		,f_pathSvr
		 		,f_pathRel
		 		,f_lenLoc
		 		,f_sizeLoc
				,f_pid
				,f_complete
		 		,f_time
		 		,f_deleted
		 		,f_pidRoot
				,f_uid
				,f_nameLoc
				,f_pathLoc
				 from up7_folders where f_id=:f_id limit 1";
	    
	    $db = new DbHelper();
	    $cmd = $db->prepare_utf8($sql);
	    $cmd->bindParam(":f_id", $id);
	    $row = $db->ExecuteRow($cmd);
	    
	    $inf = null;
	    if (!empty($row))
	    {
	        $inf = new FileInf();
	        $inf->id            = $row["f_id"];
	        $inf->pathSvr 		= $row["f_pathSvr"];
	        $inf->pathRel 		= $row["f_pathRel"];
	        $inf->lenLoc 		= intval($row["f_lenLoc"]);
	        $inf->sizeLoc 		= $row["f_sizeLoc"];
	        $inf->pid 			= $row["f_pid"];
	        $inf->complete 		= (bool)$row["f_complete"];
	        $inf->PostedTime 	= $row["f_time"];
	        $inf->deleted 		= (bool)$row["f_deleted"];
	        $inf->pidRoot 		= $row["f_pidRoot"];
	        $inf->uid 			= $row["f_uid"];
	        $inf->nameLoc 		= $row["f_nameLoc"];
	        $inf->pathLoc 		= $row["f_pathLoc"];
	    }
	    return $inf;
	}
	
	static function read2($pathRel,$id)
	{
	    $sql = "select
                 f_id   
		 		,f_pathSvr
		 		,f_pathRel
		 		,f_lenLoc
		 		,f_sizeLoc
				,f_pid
				,f_complete
		 		,f_time
		 		,f_deleted
		 		,f_pidRoot
				,f_uid
				,f_nameLoc
				,f_pathLoc
				 from up7_folders where f_pathRel=:pathRel and f_deleted=0 and f_id!=:f_id limit 1";
	    
	    $db = new DbHelper();
	    $cmd = $db->prepare_utf8($sql);
	    $cmd->bindParam(":pathRel", $pathRel);
	    $cmd->bindParam(":f_id", $id);
	    $row = $db->ExecuteRow($cmd);
	    
	    $inf = null;
	    if (!empty($row))
	    {
	        $inf = new FileInf();
	        $inf->id            = $row["f_id"];
	        $inf->pathSvr 		= $row["f_pathSvr"];
	        $inf->pathRel 		= $row["f_pathRel"];
	        $inf->lenLoc 		= intval($row["f_lenLoc"]);
	        $inf->sizeLoc 		= $row["f_sizeLoc"];
	        $inf->pid 			= $row["f_pid"];
	        $inf->complete 		= (bool)$row["f_complete"];
	        $inf->PostedTime 	= $row["f_time"];
	        $inf->deleted 		= (bool)$row["f_deleted"];
	        $inf->pidRoot 		= $row["f_pidRoot"];
	        $inf->uid 			= $row["f_uid"];
	        $inf->nameLoc 		= $row["f_nameLoc"];
	        $inf->pathLoc 		= $row["f_pathLoc"];
	    }
	    return $inf;
	}
	
	static function del($id,$uid)
	{
	    $se = new SqlExec();
	    $se->update2("up7_folders", array("f_deleted"=>1), array("f_id"=>$id,"f_uid"=>$uid));
	}
	
	static function count($id)
	{
		$sql = "select count(*) from up7_files where f_pidRoot=:id";
		$db = new DbHelper();
		$cmd = $db->GetCommand($sql);
		$cmd->bindParam(":id", $id);
		$count = $db->ExecuteScalar($cmd);
		return intval($count);
	}

	static function Clear()
	{
		$db = new DbHelper();
		$db->ExecuteNonQueryTxt("delete from up7_files;");
		$db->ExecuteNonQueryTxt("delete from up7_folders;");
	}
	
	static function Remove($fid,$uid)
	{
	    $se = new SqlExec();
	    $se->update2("up7_files", array("f_deleted"=>1), array("f_id"=>$fid,"f_uid"=>$uid));
	    $se->update2("up7_files", array("f_deleted"=>1), array("f_pidRoot"=>$fid,"f_uid"=>$uid));
	    $se->update2("up7_folders", array("f_deleted"=>1), array("f_id"=>$fid,"f_uid"=>$uid));
	}
	
	function addBatch(/*array(FileInf)*/$arr){
	    $ret = false;
	    $sb = "insert into up7_folders(".
	   	    " f_id" .
	   	    ",f_pid".
	   	    ",f_pidRoot".
	   	    ",f_nameLoc".
	   	    ",f_uid".
	   	    ",f_pathLoc".
	   	    ",f_pathSvr".
	   	    ",f_pathRel".
	   	    ",f_complete".
	   	    ") values(".
	   	    " :f_id".
	   	    ",:f_pid".
	   	    ",:f_pidRoot".
	   	    ",:f_nameLoc".
	   	    ",:f_uid".
	   	    ",:f_pathLoc".
	   	    ",:f_pathSvr".
	   	    ",:f_pathRel".
	   	    ",:f_complete".
	   	    ")";
	    
	    $db = new DbHelper();
	    
	    $cmd = $db->prepare_utf8($sb);
	    
	    foreach($arr as $f)
	    {
	        $cmd->bindValue(":f_id", $f->id,\PDO::PARAM_STR);
	        $cmd->bindValue(":f_pid", $f->pid,\PDO::PARAM_STR);
	        $cmd->bindValue(":f_pidRoot", $f->pidRoot,\PDO::PARAM_STR);
	        $cmd->bindValue(":f_nameLoc", $f->nameLoc,\PDO::PARAM_STR);
	        $cmd->bindValue(":f_uid", $f->uid,\PDO::PARAM_STR);
	        $cmd->bindValue(":f_pathLoc", $f->pathLoc,\PDO::PARAM_STR);
	        $cmd->bindValue(":f_pathSvr", $f->pathSvr,\PDO::PARAM_STR);
	        $cmd->bindValue(":f_pathRel", $f->pathRel,\PDO::PARAM_STR);
	        $cmd->bindValue(":f_complete", 1,\PDO::PARAM_INT);
	        
	        $db->ExecuteRow($cmd);
	    }
	}
	
	static function complete2($id,$uid)
	{
	    $se = new SqlExec();
	    $se->update2("up7_folders", array("f_complete"=>1), array("f_id"=>$id,"f_uid"=>$uid));
	}
}
?>